/**
 * @file AsignarResultadoPruebaBioquimicaGeneral.java
 * @brief Interfaz asigna los resultados de la prueba de bioquimica general
 *
 * Copyright (C)  - May [2011]
 *
 * @author Carlos Castilla Ruiz
 * @version 01.00
 *
 */

package interfaces;

import clinica.ClinicaView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JPanel;
import org.jdesktop.application.Action;
import clinica.Interfaces;
import GestorPruebas.*;
import Controlador.Controlador;
import java.io.IOException;
import java.sql.SQLException;
import GestorUsuarios.Paciente;


public class AsignarResultadoPruebaHemostasia extends javax.swing.JPanel {

    private ClinicaView app;
    Interfaces _anterior;
    private Hemostasia prueba;
    Pattern p = Pattern.compile("[0-9]+(\\.[0-9]+)?");
    Matcher m;
    boolean hay_errores;
    
    public AsignarResultadoPruebaHemostasia(ClinicaView a) {
        initComponents();
        app = a;
    }


    public void limpiaEntradas(Prueba p,Interfaces ant) throws java.security.NoSuchAlgorithmException,java.lang.ClassNotFoundException,java.io.IOException,java.sql.SQLException{
       _anterior=ant;
       prueba= (Hemostasia)p;

       actividad_protombina.setText("");
       p_actividad_protombina.setText("");
       t_tomblopastina_parcial_act.setText("");

       Paciente pac = (Paciente) Paciente.get(prueba.getIdPaciente());
       informacion_prueba.setText(pac.getNombre()+" "+pac.getApellidos()+", "+prueba.getTipo()+", "+prueba.getFecha());

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        p_actividad_protombina = new javax.swing.JTextField();
        t_tomblopastina_parcial_act = new javax.swing.JTextField();
        actividad_protombina = new javax.swing.JTextField();
        informacion_prueba = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos de la prueba", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma 18", 0, 18));
        jLabel3.setText("Tiempo Tombloplastina Parcial Activada");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma 18", 0, 18));
        jLabel2.setText("% Actividad Protombina");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma 18", 0, 18));
        jLabel1.setText("Actividad Protombina");
        jLabel1.setName("jLabel1"); // NOI18N

        p_actividad_protombina.setName("p_actividad_protombina"); // NOI18N

        t_tomblopastina_parcial_act.setName("t_tomblopastina_parcial_act"); // NOI18N

        actividad_protombina.setFont(new java.awt.Font("Tahoma 18 Simple", 0, 12));
        actividad_protombina.setName("actividad_protombina"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(168, 168, 168)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(p_actividad_protombina, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(t_tomblopastina_parcial_act, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(actividad_protombina, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addGap(215, 215, 215))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(actividad_protombina, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(p_actividad_protombina, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(t_tomblopastina_parcial_act, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(213, Short.MAX_VALUE))
        );

        informacion_prueba.setFont(new java.awt.Font("Tahoma", 0, 14));
        informacion_prueba.setFocusable(false);
        informacion_prueba.setName("informacion_prueba"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(AsignarResultadoPruebaHemostasia.class, this);
        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(AsignarResultadoPruebaHemostasia.class);
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(informacion_prueba, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(informacion_prueba, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public boolean comprobarFloat(javax.swing.JTextField tf,String d){
        m = p.matcher(tf.getText().toString());
        if (!m.matches()) {
            hay_errores = true;
            app.addError("El "+d+" debe ser una cifra");
            return false;
        }
        return true;
    }

    @Action
    public void aceptar() throws java.text.ParseException,GestorUsuarios.IDException,java.security.NoSuchAlgorithmException,java.sql.SQLException,java.io.IOException,java.lang.ClassNotFoundException {
        app.limpiaErrores();
        hay_errores = false;
        String exito=null;
        float f=0;

        if(actividad_protombina.getText().equals("")) {
            hay_errores = true;
            app.addError("No se ha introducido la actividad protombina.");
        }else{
            if(comprobarFloat(actividad_protombina,"actividad protombina")){
                f=Float.parseFloat(actividad_protombina.getText());
                if(f>0 && f<10000000){
                    prueba.setActividadProtombina(f);
                }else{
                    hay_errores = true;
                    app.addError("Valor incorrecto de actividad protombina.");
                }
            }
        }
        if(p_actividad_protombina.getText().equals("")) {
            hay_errores = true;
            app.addError("No se ha introducido el porcentaje de actividad protombina.");
        }else{
            if(comprobarFloat(p_actividad_protombina,"porcentaje de actividad protombina")){
                f=Float.parseFloat(p_actividad_protombina.getText());
                if(f>0 && f<10000000){
                    prueba.setActividadProtombinaPorcentaje(f);
                }else{
                    hay_errores = true;
                    app.addError("Valor incorrecto de porcentaje de actividad protombina.");
                }
            }
        }
        if(t_tomblopastina_parcial_act.getText().equals("")) {
            hay_errores = true;
            app.addError("No se ha introducido el tiempo de prontoblastina parcial activada.");
        }else{
            if(comprobarFloat(t_tomblopastina_parcial_act,"tiempo de prontoblastina parcial activada")){
                f=Float.parseFloat(t_tomblopastina_parcial_act.getText());
                if(f>0 && f<10000000){
                    prueba.setTiempoTombloplastinaParcialAct(f);
                }else{
                    hay_errores = true;
                    app.addError("Valor incorrecto de tiempo de prontoblastina parcial activada.");
                }
            }
        }
       
        if(hay_errores==true) {
            app.muestraListaErrores();
        } else {
            EstadoPrueba e=prueba.getEstadoPrueba();
            exito=Controlador.cambiarEstadoPrueba(prueba.getIdPrueba(),e.Realizada);
            if(exito==null) {
                //app.abrirMensajeExito("El estado ha sido modificado correctamente", _anterior);
            } else {
                app.abrirMensajeError(exito,_anterior);
            }
            exito=Controlador.asignarResultados(prueba);
            if(exito==null) {
                app.abrirMensajeExito("Los resultados han sido asignados correctamente", Interfaces.PRINCIPAL_TECNICO);
            } else {
                app.abrirMensajeError(exito,_anterior);
            }

        }
    }

    @Action
    public void cancelar() {
        app.volver(_anterior);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField actividad_protombina;
    private javax.swing.JTextField informacion_prueba;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField p_actividad_protombina;
    private javax.swing.JTextField t_tomblopastina_parcial_act;
    // End of variables declaration//GEN-END:variables

}
